#include "sys_util.h"
#include "command_send.h"
int utils_executor_init(bytearray_t* ba, int id, int bRsp, int bResetBa)
{
    if(bResetBa == 1)
    {
        byte_array_clear(ba);
    }
    ba->self_ival = 0;
    return byte_array_serial(ba, "wdbbbb", id, 0, 0x57, 0x13, bRsp, 0);
}
// 向bytearray 中追加一条子命令
int utils_executor_append(bytearray_t* ba, const char* format, ...)
{
    va_list argp;
    int count = 0;
    va_start(argp, format);
    count = byte_array_vserial_pre_cnt(ba, format, argp);
    va_end(argp);
    if(count > 0)
    {
        ba->self_ival += 1;
    }
    return 0;
}
void utils_exector_cmd_inc(bytearray_t* ba)
{
    ba->self_ival += 1;
}
int utils_reader_append(bytearray_t* ba, const char* format, ...)
{
    va_list argp;
    int count = 0;
    va_start(argp, format);
    count = byte_array_vserial(ba, format, argp);
    va_end(argp);
    if(count > 0)
    {
        ba->self_ival += 1;
    }
    return 0;
}
static void _set_exector_cmd_cnt(bytearray_t* ba, int index)
{
    int ba_length = ba->len;
    ba->len = index;
    byte_array_serial(ba,"b",  ba->self_ival & 0Xff);
    ba->len = ba_length;
}
int utils_executor_send(bytearray_t* ba)
{
    // set command count
    _set_exector_cmd_cnt(ba, 9);
     command_send(ba);
     return ba->self_ival;
}
int utils_reader_init(bytearray_t* ba, int id, int sig,  int bResetBa)
{
    if(bResetBa == 1)
    {
        byte_array_clear(ba);
    }
    ba->self_ival = 0;
    return byte_array_serial(ba, "wdbbwb", id, 0, 0x57, 0x16, sig, 0);
}

int utils_reader_send(bytearray_t* ba)
{
    _set_exector_cmd_cnt(ba, 10);
     command_send(ba);
     return ba->self_ival;
}

